package common

type KubeConfig struct {
	ApiVersion     string         `json:"apiVersion" yaml:"apiVersion"`
	Kind           string         `json:"kind" yaml:"kind"`
	CurrentContext string         `json:"current-context" yaml:"current-context"`
	Clusters       []*Cluster     `json:"clusters" yaml:"clusters"`
	Users          []*User        `json:"users" yaml:"users"`
	Contexts       []*Context     `json:"contexts" yaml:"contexts"`
	Preferences    map[string]any `json:"preferences" yaml:"preferences"`
}

type Cluster struct {
	Name    string       `json:"name" yaml:"name"`
	Cluster *ClusterInfo `json:"cluster" yaml:"cluster"`
}

type ClusterInfo struct {
	Server                   string `json:"server" yaml:"server"`
	CertificateAuthorityData string `json:"certificate-authority-data" yaml:"certificate-authority-data"`
}

type User struct {
	Name string    `json:"name" yaml:"name"`
	User *UserInfo `json:"user" yaml:"user"`
}

type UserInfo struct {
	ClientCertificateData string `json:"client-certificate-data" yaml:"client-certificate-data"`
	ClientKeyData         string `json:"client-key-data" yaml:"client-key-data"`
	Token                 string `json:"token" yaml:"token"`
}

type Context struct {
	Name    string      `json:"name" yaml:"name"`
	Context ContextInfo `json:"context" yaml:"context"`
}

type ContextInfo struct {
	Cluster   string `json:"cluster" yaml:"cluster"`
	User      string `json:"user" yaml:"user"`
	Namespace string `json:"namespace" yaml:"namespace"`
}

func (c KubeConfig) SelectCurrentContext() *Context {
	for _, context := range c.Contexts {
		if context.Name == c.CurrentContext {
			return context
		}
	}
	return nil
}
